package com.bridgeintelligent.tag.bulkload.service.es.query;

import com.bridgeintelligent.tag.bulkload.api.fileload.FilterGroup;
import com.bridgeintelligent.tag.bulkload.service.es.query.condition.BreadthFirstQueue;
import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

/**
 * Created by JackCheung on 2019/10/14 2:07 PM.
 */
public class EsQueryBuilder {

    /**
     * filterGroup转成QueryBuilder
     *
     * @param filterGroup filterGroup
     * @return QueryBuilder
     */
    public static QueryBuilder buildFilter(FilterGroup filterGroup) {
        boolean isFilterEmpty = null == filterGroup || (CollectionUtils.isEmpty(filterGroup.getFilters()) && CollectionUtils.isEmpty(filterGroup.getChildren()));
        if (isFilterEmpty) {
            return QueryBuilders.matchAllQuery();
        }
        return BreadthFirstQueue.transform(filterGroup);
    }
}
